iT邦幫忙

2025 iThome 鐵人賽

DAY 3
0

我的助手介面是用customtkinter做的,所以今天來簡單介紹一下

常見元件的應用範例

1. 文字方塊 (Label)

  • 用途:顯示不可編輯的文字,通常用於標題、說明或提示。
  • 類別customtkinter.CTkLabel

2. 輸入框 (Entry)

  • 用途:讓使用者輸入單行文字。
  • 類別customtkinter.CTkEntry

3. 按鈕 (Button)

  • 用途:讓使用者點擊以觸發特定動作。
  • 類別customtkinter.CTkButton

4. 文字方塊 (Text Box)

  • 用途:顯示或讓使用者輸入多行文字。
  • 類別customtkinter.CTkTextbox(這在舊版 Tkinter 中是 Text

5. 選擇框 (Checkbox)

  • 用途:讓使用者勾選或取消勾選。
  • 類別customtkinter.CTkCheckBox

在 Tkinter 和 CustomTkinter 中,常用的字體樣式有:

  • "bold":粗體
  • "italic":斜體
  • "underline":底線
  • "overstrike":刪除線

設定主題外觀,這裡我們使用淺色模式

  • customtkinter.set_appearance_mode("light")

同時設定粗體和斜體,字型為 Arial,大小為 20,可以這樣寫:

  • font=("Arial", 20, "bold italic")

設定顏色

  • text_color="#ff5733"

範例

import customtkinter

def button_click():
    input_text = entry.get()
    print(f"輸入框的內容:{input_text}")

# 設定主題外觀
customtkinter.set_appearance_mode("light")

# 建立主視窗
app = customtkinter.CTk()
app.title("客製化元件範例")
app.geometry("500x300")

# 1. 客製化文字標籤 (Label)
# 改變文字顏色、大小和字體
title_label = customtkinter.CTkLabel(
    app,
    text="CustomTkinter 客製化",
    text_color="#ff5733",  # 使用十六進位顏色碼,橙紅色
    font=("timesnewroman", 28, "italic") # 字體, 大小, 樣式
)
title_label.pack(pady=20)

# 2. 客製化輸入框 (Entry)
# 改變背景顏色和圓角
entry = customtkinter.CTkEntry(
    app,
    placeholder_text="在這裡輸入",
    fg_color="#e0e0e0",  # 淺灰色
    corner_radius=10,
    width=300
)
entry.pack(pady=10)

# 3. 客製化按鈕 (Button)
# 改變背景顏色、滑鼠停留顏色和文字顏色
button = customtkinter.CTkButton(
    app,
    text="提交",
    command=button_click,
    fg_color="#4CAF50",  # 綠色
    hover_color="#c8d628", # 滑鼠停留時的顏色
    text_color="#ffffff",  # 白色
    corner_radius=15,
    width=200
)
button.pack(pady=20)

app.mainloop()

https://ithelp.ithome.com.tw/upload/images/20250917/201779217RL9dNouOv.png


上一篇
DAY 2
系列文
我的 AI 助手開發3
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言